package com.activity.dao.impl;

import com.activity.bean.Seckill;
import com.activity.bean.SeckillExample;
import com.activity.mapper.SeckillMapper;
import com.activity.dao.SeckillDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by wangzhanjin on 2017/12/26.
 */
@Repository
public class SeckillDaoImpl implements SeckillDao {
    @Autowired
    private SeckillMapper seckillMapper;



    public Seckill queryById(long seckillId) {
        return seckillMapper.selectByPrimaryKey(seckillId);
    }

    public int redeuceNumber(long seckillId, Date killTime) {
        Seckill seckill = seckillMapper.selectByPrimaryKey(seckillId);
        if(seckill==null||seckill.getStartTime().after(killTime)){
            return -1;
        }
        Integer number = seckill.getNumber();
        if(number<=0){
            return -1;
        }
        seckill.setNumber(number-1);
        return seckillMapper.updateByPrimaryKey(seckill);
    }

    public List<Seckill> queryAll(int offet, int limit) {

        return seckillMapper.selectByExample(new SeckillExample());
    }
}
